package com.sino.struts2;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sino.business.IAclService;
import com.sino.business.IExpertManageService;
import com.sino.business.IPatentManageService;
import com.sino.business.service.LoginInfo;
import com.sino.model.ExpertModel;
import com.sino.model.ModuleModel;
import com.sino.util.SysParameter;

public class LoginAction extends ActionSupport {
	private IPatentManageService ipatentManageService;

	private IExpertManageService exmpertManageService;

	private IAclService aclServiceImp;

	private String name;

	private String password;

	private LoginInfo logininfo;

	private String identity;

	private List<ModuleModel> moduleList;

	private String entityType;

	private String currentYear = new Integer(Calendar.getInstance().get(
			Calendar.YEAR)).toString();

	public IExpertManageService getExmpertManageService() {
		return exmpertManageService;
	}

	public void setExmpertManageService(
			IExpertManageService exmpertManageService) {
		this.exmpertManageService = exmpertManageService;
	}

	/**
	 * @param aclServiceImp
	 *            the aclServiceImp to set
	 */
	public void setAclServiceImp(IAclService aclServiceImp) {
		this.aclServiceImp = aclServiceImp;
	}

	public LoginInfo getLogininfo() {
		return logininfo;
	}

	public void setLogininfo(LoginInfo logininfo) {
		this.logininfo = logininfo;
	}

	/**
	 * @return the moduleList
	 */
	public List<ModuleModel> getModuleList() {
		return moduleList;
	}

	/**
	 * @param moduleList
	 *            the moduleList to set
	 */
	public void setModuleList(List<ModuleModel> moduleList) {
		this.moduleList = moduleList;
	}

	/**
	 * @return the entityType
	 */
	public String getEntityType() {
		return entityType;
	}

	/**
	 * @param entityType
	 *            the entityType to set
	 */
	public void setEntityType(String entityType) {
		this.entityType = entityType;
	}

	public String login() {

		if (name == null || password == null || name.trim().equals("")
				|| password.trim().equals("")) {
			addFieldError("user", "用户名或密码不能为空！");
			if ("1".equals(entityType)) {
				return INPUT;
			} else {
				return "profess_input";
			}
		} else {
			SysParameter.setCURRENT_YEAR(currentYear);
			if ("1".equals(entityType)) {
				logininfo = this.getIpatentManageService().getUsersByName(name,
						password);
				if (logininfo != null) {
					Map session = ActionContext.getContext().getSession();
					session.clear();
					session.put("logininfo", logininfo);
					session.put("CURRENTYEAR", currentYear);
					session.put("entityType", entityType);
					return SUCCESS;
				}
			} else if ("0".equals(entityType)) {
				ExpertModel em = this.getExmpertManageService()
						.checkExpertName(name, password);
				if (em != null) {
					Map session = ActionContext.getContext().getSession();
					session.clear();
					logininfo = new LoginInfo();
					session.put("expertModel", em);
					session.put("identity", identity);
					session.put("logininfo", logininfo);
					session.put("CURRENTYEAR", currentYear);
					session.put("entityType", entityType);
					return SUCCESS;
				}
			} else {
				addFieldError("user", "用户名或密码错误或者登陆类型错误！");
				if ("1".equals(entityType)) {
					return INPUT;
				} else {
					return "profess_input";
				}
			}

		}
		addFieldError("user", "用户名或密码错误或者登陆类型错误！");
		if ("1".equals(entityType)) {
			return INPUT;
		} else {
			return "profess_input";
		}
	}

	//
	// public String loginExperts() {
	//
	// if (name == null || password == null || name.trim().equals("")
	// || password.trim().equals("")) {
	// addFieldError("user", "用户名或密码不能为空！");
	// return INPUT;
	// } else {
	// ExpertModel em = this.getExmpertManageService().checkExpertName(
	// name, password);
	// if (em != null) {
	// Map session = ActionContext.getContext().getSession();
	// session.clear();
	// logininfo = new LoginInfo();
	// session.put("expertModel", em);
	// session.put("identity", identity);
	// session.put("logininfo", logininfo);
	// return SUCCESS;
	// } else {
	// addFieldError("user", "用户名或密码错误！");
	// return INPUT;
	// }
	//
	// }
	//
	// }

	// 退出
	public String loginout() throws IOException {
		HttpSession session = ServletActionContext.getRequest().getSession();

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html; charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<script language='javascript'>");
		out.println("if(confirm('确认退出系统？')){");
		out.println("top.opener=null;");
		out.println("top.close();");
		out.println("}");
		out.println("</script>");
		session.removeAttribute("zlid");
		session.removeAttribute("logininfo");
		return null;
	}

	public String relogin() throws IOException {
		HttpSession session = ServletActionContext.getRequest().getSession();
		String enTe = session.getAttribute("entityType").toString();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<script language='javascript'>");
		out.println("top.opener=null;");
		out.println("top.close();");
		if ("1".equals(enTe)) {
			out.println("window.open('../admin/login.jsp');");
		}
		else{
			out.println("window.open('../admin/profess_login.jsp');");
		}
		out.println("</script>");
		session.removeAttribute("zlid");
		session.removeAttribute("logininfo");
		return null;
	}

	public String showMenu() {
		if ("1".equals(entityType)) {
			LoginInfo li = (LoginInfo) ActionContext.getContext().getSession()
					.get("logininfo");
			moduleList = this.aclServiceImp.listPermissionByEntity(new Integer(
					entityType), li.getId());
		} else if ("0".equals(entityType)) {
			ExpertModel em = (ExpertModel) ActionContext.getContext()
					.getSession().get("expertModel");
			moduleList = this.aclServiceImp.listPermissionByEntity(new Integer(
					entityType), em.getFBh());
		}
		return SUCCESS;
	}

	public IPatentManageService getIpatentManageService() {
		return ipatentManageService;
	}

	public void setIpatentManageService(
			IPatentManageService ipatentManageService) {
		this.ipatentManageService = ipatentManageService;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setUsername(String username) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getIdentity() {
		return identity;
	}

	public void setIdentity(String identity) {
		this.identity = identity;
	}

	/**
	 * @return the currentYear
	 */
	public String getCurrentYear() {
		return currentYear;
	}

	/**
	 * @param currentYear
	 *            the currentYear to set
	 */
	public void setCurrentYear(String currentYear) {
		this.currentYear = currentYear;
	}

}